<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>模板</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    html, body {
      width: 100%;
      height: 100%;
      display: flex;
      place-content: center;
      place-items: center;
    }
  </style>
</head>
<body>
  <script>
    // 在浏览器端如何加密
    async function encryptChunk(chunk, key) {
      // iv是加密过程中一个随机值，用于确保就是使用相同的密钥对相同的数据进行加密，每次加密的结果也不一样
      // iv是一个12个字节的随机数组做为初始化的向量
      const algorithm = {
        name: 'AES-GCM',
        iv: window.crypto.getRandomValues(new Uint8Array(12))
      }
      const encryptChunk = await window.subtle.encrypt(algorithm, key, chunk)
      return { encryptChunk, iv: algorithm.iv }
    }
    async function generateKey() {
      const key = await window.crypto.subtle.generateKey({
        name: 'AES-GCM',
        length: 256
      }, true, ['encrypt', 'decrypt'])
      return key
    }
    const key = await generateKey()
    const { encryptChunk, iv } = await encryptChunk(chunk, key)
  </script>
</body>
</html>